<template>
    <div class="QRcode">
        <div>
            <img :src="url">
            <p>请使用 网易云音乐APP 扫码登录</p>
            <p>扫描二维码，登录更快捷</p>
        </div>
    </div>
</template>

<script>
import {
    QRkey,
    QRcreate,
    QRcheck
} from '@/library/api/api'
export default {
    name:'QRcode',
    data() {
        return {
            key:"",
            qrimg:"qrimg",
            url:"",
            timestamp:1
        }
    },
    methods: {
        times(){
            let num = 1;
            let interval = setInterval(function () {
                num += 1
                // console.log(this.timestamp);
                return this.timestamp = num
            }, 1000);
            if(this.timestamp=="5000") {
                clearInterval(interval);
            }
        }
    },
    // updated() {
    //     this.times()
    // },
    created() {
        this.times()
        QRkey({
            data: {
                timestamp:this.timestamp
            }
        })
        .then(res=>{
            this.key = res.data.unikey
            console.log(this.timestamp);
            return this.key
        }),
        QRcreate({
            data: {
                key:this.key,
                qrimg:this.qrimg
            }
        }).then(res=>{
            this.url = res.data.qrimg
        }),
        QRcheck({
            data: {
                key:this.key
            }
        }).then(res=>{
            console.log(res);
        }).catch(error=>{
            // console.log(error.response.data);
            if(error.response.data.code=="800") {
                alert(error.response.data.message)
            }
        })
    }
}
</script>

<style lang="scss" scoped>
@import './QRcode.scss'
</style>
